using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

using Pampa.Grid.UI.CustomControls;

/// <summary>
/// Summary description for GridConfigurationController
/// </summary>
public class GridConfigurationController
{
    public GridConfigurationController()
    {
    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public static string[] List()
    {
        string configPath = GridConfigurationController.ConfigPath;
        string[] files = System.IO.Directory.GetFiles(configPath);
        string[] ret = new string[files.Length];

        for (int i = 0; i < files.Length; i++ )
        {
            ret[i] = files[i].Replace(configPath, "").Replace(".config.xml", "");
        }

        return ret;
    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public static void Save(string gridID, string configName)
    {
        XmlDocument doc = CustomGrid.GetCurrentXmlConfiguration(gridID);
        doc.Save(GetFileName(configName));
    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public static void Load(string gridID, string configName)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(GetFileName(configName));

        CustomGrid grid = CustomGrid.LoadFromSession(gridID);
        grid.Configuration.Update((XmlNode)doc.FirstChild.FirstChild);
        grid.ShowData = true;
    }

    private static string GetFileName(string configName)
    {
        return ConfigPath + configName + ".config.xml";
    }

    private static string ConfigPath
    {
        get 
        { 
            string appPath = HttpContext.Current.Request.ApplicationPath;
            string rootPath = HttpContext.Current.Request.MapPath(appPath);
            return rootPath + "\\Demo\\GridConfig\\";
        }
    }
}
